﻿using AppAdmin;
using BusinessLogic;
using Common;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AppAdminWeb.Controllers
{
    public class templetinfoController : BaseController
    {
        //public new ActionResult Index()
        //{
        //    return View();
        //}

        public ActionResult SetMap()
        {
            return View();
        }

        public ActionResult Form()
        {
            string keyValue = Request.QueryString["keyValue"];
            return View();
        }

        public ActionResult Details()
        {
            string keyValue = Request.QueryString["keyValue"];
            return View();
        }


        [HttpGet]
        public ActionResult GetGridJson(Pagination pagination, string keyword, string type)
        {
            var expression = ExtLinq.True<templetinfo>();
            expression = expression.And(t => t.is_delete == 0);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.micro_title.Contains(keyword.Trim()));
            }
            if (!string.IsNullOrEmpty(type))
            {
                expression = expression.And(t => t.category_id  == type );
            }

            int Total = 0;
            var list = new templetinfoLogic().getPageDate<templetinfo, int>(c => new { c.micro_id, c.micro_title, c.remark, c.order_by, c.share_describe, c.share_path, c.share_title, c.background_bottom, c.background_height, c.background_width, c.background_path, c.brand_id, c.collect_number, c.copy_text, c.cover_path, c.create_time, c.false_click, c.is_delete, c.is_dynamic, c.is_enable, c.is_top, c.ispay, c.isvip, c.tipshow, c.true_click, c.update_time,c.category_id }, expression, c => c.micro_id, pagination.page, pagination.rows, out Total);
            pagination.records = Total;
            var data = new
            {
                rows = list,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = new templetinfoLogic().GetForm(Convert.ToInt32(keyValue));
            return Content(data.ToJson());
        }
        [HttpPost]
        public ActionResult SubmitForm(FormCollection collection, string keyValue)
        {
            templetinfo model = new templetinfo();
            templetinfoLogic bll = new templetinfoLogic();
            model.micro_title = collection["micro_title"].Trim("&.nbsp;".ToCharArray());
            model.remark = collection["remark"].Trim("&.nbsp;".ToCharArray());
            //model.share_describe = collection["share_describe"].Trim("&.nbsp;".ToCharArray());
            //model.share_path = collection["share_path"].Trim("&.nbsp;".ToCharArray());
            model.share_title = collection["share_title"].Trim("&.nbsp;".ToCharArray());
            model.background_bottom = collection["Pic_f_background_bottom"].Trim("&.nbsp;".ToCharArray());
            model.background_height =Convert.ToInt32(collection["background_height"]);
            model.background_width = Convert.ToInt32(collection["background_width"]);
            
            model.background_path = collection["Pic_f_background_path"].Trim("&.nbsp;".ToCharArray()); ;
            model.copy_text = "";
            model.cover_path = collection["Pic_f_cover_path"].Trim("&.nbsp;".ToCharArray());

            //model.City = collection["seType"].Trim();
            model.is_enable = Convert.ToByte(collection["HD_IsShow"].Trim());
            model.category_id =  collection["seType"].Trim() ;
            //model.Details = collection["Details"].Trim("&.nbsp;".ToCharArray());
            //model.Tel = collection["Tel"].Trim("&.nbsp;".ToCharArray());

            string urls = "";// collection["urls"].Trim("&.nbsp;".ToCharArray());
            if (!string.IsNullOrEmpty(keyValue))//修改
            {
                model.update_time = DateTime.Now;
                model.micro_id = Convert.ToInt32(keyValue);
                var tem = bll.UpdateInfo(model);
                if (tem > 0 && !string.IsNullOrEmpty(urls))//修改成功
                {
                    //bll.Pic_Shop(urls, model.ID);
                }
            }
            else
            {
                model.update_time = DateTime.Now;
                model.create_time = DateTime.Now;

                model.brand_id = Guid.NewGuid().ToString();
                var tem = bll.AddInfo(model);
                if (tem > 0 && !string.IsNullOrEmpty(urls))
                {
                    //bll.Pic_Shop(urls, tem);
                }
            }


            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            templetinfoLogic bll = new templetinfoLogic();
            bll.DelInfo(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "删除成功。" }.ToJson());
        }
        public ActionResult Show(string keyValue)
        {
            templetinfoLogic bll = new templetinfoLogic();
            bll.Show(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        public ActionResult notShow(string keyValue)
        {
            templetinfoLogic bll = new templetinfoLogic();
            bll.UnShow(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        [HttpGet]
        public ActionResult UploadPic()
        {
            return View();
        }
    }
}